package cn.yitun.chat.yitunserverimapicore.business.chat.service.impl;

import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.LastChatGroup;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Item;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Section;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.LastChatDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.userchat.LastChatInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupChatContentVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupChatItemVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.LastChatVo;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.UserChatTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.LastChatGroupService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupChatContentDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupChatItemDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupDao;
import cn.yitun.chat.yitunserverimbase.dao.userchat.LastChatDao;
import cn.yitun.chat.yitunserverimbase.dao.userchat.UserChatContentDao;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LastChatGroupServiceImpl
		extends CrudServiceImpl<LastChatDao, LastChatDto, LastChatInfo, LastChatVo>
		implements LastChatGroupService{

	@Resource
	private GroupChatContentDao groupChatContentDAO;
	@Resource
	private UserChatContentDao userChatDAO;
	@Resource
	private GroupChatItemDao groupChatItemDAO;
	@Resource
	private GroupDao groupDAO;

	/**
	 * 获取最后聊天的列表
	 *
	 * @param userId
	 * @return
	 */
	public List<LastChatGroup> getLastChatWithContentList(Long userId, int pageNo, int pageSize) {

		List<LastChatVo> list = dao.listVO(
				R.param("userId", userId)
				.put("type", UserChatTypeEnums.TYPE_GROUP.getType())
				);

		List<Long> ids = new ArrayList<>();
		for (LastChatVo ulc : list) {
			ids.add(ulc.getContentId());
		}
		PageHelper.startPage(pageNo, pageSize);
		List<GroupChatContentVo> userContentList = groupChatContentDAO.listVO(
				R.param("ids", ids)
				);
		List<LastChatGroup> contents = getLastChatGroupList(userContentList, false);
		return contents;
	}

	/**
	 * 删除某聊天室最后聊天记录
	 * @param userId
	 * @param chatId
	 * @return
	 */
	public boolean remove(Long userId, Long chatId) {
		boolean mark = 0 < dao.deleteBy(userId, chatId, UserChatTypeEnums.TYPE_GROUP.getType());
		return mark;
	}

	/**
	 * 获取最后聊天记录
	 * @param chatContentList
	 * @param inverted
	 * @return
	 */
	public List<LastChatGroup> getLastChatGroupList(List<GroupChatContentVo> chatContentList, boolean inverted) {

		List<LastChatGroup> contents = new ArrayList<>();

		List<Long> contentIds = new ArrayList<>();
		for (GroupChatContentVo rcc : chatContentList) {
			contentIds.add(rcc.getId());
		}
		Map<Long, List<GroupChatItemVo>> chatItemMap = new HashMap<>();
		if (!contentIds.isEmpty()) {
			List<GroupChatItemVo> chatItemList = groupChatItemDAO.getGroupChatItemListByContentIds(contentIds);
			for (GroupChatItemVo rci : chatItemList) {
				List<GroupChatItemVo> list = chatItemMap.get(rci.getMessageId());
				if (null == list) {
					list = new ArrayList<>();
					chatItemMap.put(rci.getGroupChatContentId(), list);
				}
				list.add(rci);
			}
		}

		for (GroupChatContentVo rcc : chatContentList) {

			Long groupId = rcc.getGroupId();
			Long messageKey = rcc.getMessageId();
			Long contentId = rcc.getId();

			// TODO
			LastChatGroup map = new LastChatGroup();
			GroupChatContentVo content = new GroupChatContentVo();
			UserVo userData = new UserVo();

			userData.setHead(rcc.getUserHead());
			userData.setId(rcc.getUserId());
			userData.setName(rcc.getUserName());
			userData.setNickname(rcc.getUserNickname());
			// userData.setRole(rcc.getUserRole());
			List<GroupChatItemVo> list = chatItemMap.get(rcc.getId());
			List<Section> sections = new ArrayList<>();
			if (null != list && !list.isEmpty()) {
				int index = -1;
				Section section;
				List<Item> items = null;
				for (GroupChatItemVo rci : list) {
					if (index != rci.getSection()) {
						index = rci.getSection();

						items = new ArrayList<>();
						section = new Section();
						section.setItems(items);
						sections.add(section);
					}

					Item item = new Item();
					item.setType(rci.getType());
					item.setValue(rci.getFilterValue());
					if (null != items) {
						items.add(item);
					}
				}
				content.setMessageId(messageKey);
				content.setSections(sections);

				map.setUser(userData);

				// TODO
				map.setGroup(groupDAO.getVO(groupId));
				map.setMessageId(messageKey);
				map.setContentId(contentId);
				map.setContent(content);

				if (inverted) {
					contents.add(0, map);
				} else {
					contents.add(map);
				}
			}
		}
		return contents;
	}
}
